package com.superm.controller;

import com.alibaba.fastjson.JSON;
import com.superm.entity.Emp;
import com.superm.entity.RepostoryType;
import com.superm.mappers.EmpMapper;
import com.superm.mappers.RepostoryMapper;
import com.superm.service.EmpService;
import com.superm.service.RepostoryService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/repostory")
public class RepostoryController {
    @Autowired
    private RepostoryService repostoryService;
    @Autowired
    private EmpService empService;

    @RequestMapping(path = "/index")
    public ModelAndView index(){//初始化进入  仓库页面
        ModelAndView ma = new ModelAndView();
        List<RepostoryType> reps = repostoryService.allRepostory();
        ma.addObject("reps", JSON.toJSONString(reps));
        ma.setViewName("repostory/allRepostory");
        return ma;
    }
    @RequestMapping(path = "/allRep")
    @ResponseBody//展示所有仓库信息
    public Map<String,Object> allRepostory(){
        Map<String,Object> map = new HashMap<>();
        List<RepostoryType> reps = repostoryService.allRepostory();
        map.put("code",0);
        map.put("data",reps);
        return map;
    }

    @GetMapping (path = "/addRep")
    public ModelAndView addRep(){
        ModelAndView ma = new ModelAndView();
        List<Emp> emps= empService.allEmpRep();
        ma.addObject("emps",emps);
        ma.setViewName("repostory/addRepostory");
        return ma;
    }
    @PostMapping(path = "/addRep")
    @ResponseBody//添加仓库信息
    public Map<String,Object> addRep(RepostoryType rep){
        Map<String,Object> map = new HashMap<>();
        if(repostoryService.addRepostory(rep)>0){
            System.out.println(rep);
            map.put("success",true);
            map.put("msg","添加成功");
        }else{
            map.put("success",false);
            map.put("msg","添加失败");
        }
        return map;
    }
    @GetMapping (path = "/upRep")
    public ModelAndView upRep(Integer repid){
        ModelAndView ma = new ModelAndView();
        ma.addObject("rep",repostoryService.queryRid(repid));
        ma.addObject("emps",empService.allEmpRep());
        ma.setViewName("repostory/upRepostory");
        return ma;
    }

    /**
     * 异步查询仓库信息
     * @return
     */
    @RequestMapping("/ajaxSelectIdRepostory")
    @ResponseBody
    public Map<String,Object> ajaxSelectIdRepostory(@RequestParam("rid") Integer rid){
        Map<String,Object> map =repostoryService.ajaxQuertRid(rid);
        return map;

    }

    @PostMapping(path = "/upRep")
    @ResponseBody//添加仓库信息
    public Map<String,Object> upRep(RepostoryType rep){
        System.out.println("SSSSSSSSSSSSSSSSSS");
        System.out.println(rep);
        Map<String,Object> map = new HashMap<>();
        if(repostoryService.upRepostory(rep)>0){
            map.put("success",true);
            map.put("msg","修改成功");
        }else{
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }
    @RequestMapping(path = "/delRep")
    @ResponseBody//添加仓库信息
    public Map<String,Object> delRep(Integer repid){
        System.out.println(repid+"Ssssssssssssss");
        Map<String,Object> map = new HashMap<>();
        if(repostoryService.delRepostory(repid)>0){
            map.put("success",true);
            map.put("msg","删除成功");
        }else{
            map.put("success",false);
            map.put("msg","删除失败");
        }
        return map;
    }
}
